package org.ea4rct.ealog.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import org.ea4rct.ealog.core.Modes;
import org.ea4rct.ealog.core.QSO;

public class GuardBook extends ViewPart {
	
	public static final String ID = "org.ea4rct.ealog.views.GuardBook";
	
	private Text contactNumberText;
	
	private Text dateText;
	
	private Text timeText;
	
	private Text callText;
	
	private Text frequencyText;
	
	private Spinner RSTSentSpinner;
	
	private Spinner RSTReceivedSpinner;
	
	private Combo modeCombo;
	
	private Text powerText;
	
	private Text commentsText;
	
	private Button addQSOButton;
	
	private Button deleteQSOButton;
	
	private Button cancelButton;
	
	private Table table;

	public GuardBook() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
        GridLayout layout = new GridLayout();
        container.setLayout(layout);
        layout.numColumns = 10;
        layout.horizontalSpacing = 9;
        layout.verticalSpacing = 9;
        
        /*
         * USER ZONE
         */
        Label label1 = new Label(container, SWT.NULL);
        label1.setText("nº");
        Label label2 = new Label(container, SWT.NULL);
        label2.setText("Date");
        Label label3 = new Label(container, SWT.NULL);
        label3.setText("Time");
        Label label4 = new Label(container, SWT.NULL);
        label4.setText("Callsign");
        Label label5 = new Label(container, SWT.NULL);
        label5.setText("Frequency");
        Label label6 = new Label(container, SWT.NULL);
        label6.setText("RST Sent");
        Label label7 = new Label(container, SWT.NULL);
        label7.setText("RST Received");
        Label label8 = new Label(container, SWT.NULL);
        label8.setText("Mode");
        Label label9 = new Label(container, SWT.NULL);
        label9.setText("Power");
        Label label10 = new Label(container, SWT.NULL);
        label10.setText("Comments");
        
        contactNumberText = new Text(container, SWT.BORDER | SWT.SINGLE);
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        contactNumberText.setLayoutData(gd);
        contactNumberText.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                        dialogChanged();
                }
        });
        
        dateText = new Text(container, SWT.BORDER | SWT.SINGLE);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        dateText.setLayoutData(gd);
        dateText.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                        dialogChanged();
                }
        });
        
        timeText = new Text(container, SWT.BORDER | SWT.SINGLE);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        timeText.setLayoutData(gd);
        timeText.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                        dialogChanged();
                }
        });
        
        callText = new Text(container, SWT.BORDER | SWT.SINGLE);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        callText.setLayoutData(gd);
        callText.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                        dialogChanged();
                }
        });
        
        frequencyText = new Text(container, SWT.BORDER | SWT.SINGLE);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        frequencyText.setLayoutData(gd);
        frequencyText.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                        dialogChanged();
                }
        });
        
        RSTSentSpinner = new Spinner (container, SWT.BORDER);
        RSTSentSpinner.setIncrement(1);
        RSTSentSpinner.setPageIncrement(1);
        RSTSentSpinner.setSelection(59);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        RSTSentSpinner.setLayoutData(gd);
        RSTSentSpinner.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                        dialogChanged();
                }
        });
        
        RSTReceivedSpinner = new Spinner (container, SWT.BORDER);
        RSTReceivedSpinner.setIncrement(1);
        RSTReceivedSpinner.setPageIncrement(1);
        RSTReceivedSpinner.setSelection(59);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        RSTReceivedSpinner.setLayoutData(gd);
        RSTReceivedSpinner.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                        dialogChanged();
                }
        });
        
        modeCombo = new Combo(container, SWT.READ_ONLY);
        modeCombo.setItems (new String [] {Modes.CW, Modes.LSB, Modes.USB, Modes.AM,
        		Modes.FM, Modes.PSK, Modes.FSK, Modes.SSTV});
        modeCombo.select(0);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        modeCombo.setLayoutData(gd);
        modeCombo.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                        dialogChanged();
                }
        });
        
        powerText = new Text(container, SWT.BORDER | SWT.SINGLE);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        powerText.setLayoutData(gd);
        powerText.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                        dialogChanged();
                }
        });
        
        commentsText = new Text(container, SWT.BORDER | SWT.SINGLE);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        commentsText.setLayoutData(gd);
        commentsText.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                        dialogChanged();
                }
        });
        
        addQSOButton = new Button(container,SWT.PUSH);
        addQSOButton.setText("Add");
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 2;
        addQSOButton.setLayoutData(gd);
        addQSOButton.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                	if(table.getSelectionCount() > 0){
                		addQSOButtonPushed(true);
                	}
                	else{
                		addQSOButtonPushed(false);
                	}
                }
        });
        
        deleteQSOButton = new Button(container,SWT.PUSH);
        deleteQSOButton.setText("Delete");
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 2;
        deleteQSOButton.setLayoutData(gd);
        deleteQSOButton.setEnabled(false);
        deleteQSOButton.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                        deleteButtonPushed();
                }
        });
        
        cancelButton = new Button(container,SWT.PUSH);
        cancelButton.setText("cancel");
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 2;
        cancelButton.setLayoutData(gd);
        cancelButton.setEnabled(false);
        cancelButton.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                        cancelButtonPushed();
                }
        });
        
   

        Label separator = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        separator.setLayoutData(gd);
        gd.horizontalSpan = 10;   
        
        /*
         *  GUARD BOOK TABLE
         */
        table = new Table (container, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
    	table.setLinesVisible (true);
    	table.setHeaderVisible (true);
    	GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
    	data.heightHint = 200;
    	data.horizontalSpan = 10;
    	table.setLayoutData(data);
    	
    	table.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				QSOSelected();
				
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				QSOSelected();
				
			}
		});
    	
    	//This creates the headers for the Table
    	String[] titles = {"nº", "Date", "Time(UTC)", "Call", "Frecuency", "RST Sent", "RST Received", "Mode", "Power","Comments"};
    	
    	for (String s : titles){
    		TableColumn column = new TableColumn (table, SWT.NONE);
    		column.setText(s);
    	}	
    	
    	for(TableColumn t : table.getColumns()){
    		t.pack();    		
    	}

	}
	
	/*
	 * Attending events methods
	 */
	
	
	private void addQSOButtonPushed(boolean modify) {
		if(modify){
            TableItem item = table.getSelection()[0];
            item.setText(0,contactNumberText.getText());
            item.setText(1,dateText.getText());
            item.setText(2,timeText.getText());
            item.setText(3,callText.getText());
            item.setText(4,frequencyText.getText());
            item.setText(5,RSTSentSpinner.getText());
            item.setText(6,RSTReceivedSpinner.getText());
            item.setText(7, modeCombo.getText());
            item.setText(8, powerText.getText());
            item.setText(9, commentsText.getText());
            
		}
    	else{
    		QSO q = new QSO(contactNumberText.getText(),
    				dateText.getText(), timeText.getText(),
    				callText.getText(), RSTSentSpinner.getText(),
    				RSTReceivedSpinner.getText(),frequencyText.getText(),
    				modeCombo.getText(), powerText.getText(),
    				commentsText.getText());
            addQSO(q);                                
    	}
		clearForm();		
	}
	
	

	private void addQSO(QSO q) {		
		TableItem item  = new TableItem(table, SWT.NONE, 0);
        item.setText(q.getText());
	}

	private void deleteButtonPushed() {
		table.remove(table.getSelectionIndices());
        clearForm();
	}
	
	private void cancelButtonPushed() {
		clearForm();
	}	
	
	private void dialogChanged(){
		
	}
	
	private void QSOSelected(){
        TableItem item = table.getSelection()[0];        
        contactNumberText.setText(item.getText(0));
		dateText.setText(item.getText(1));
		timeText.setText(item.getText(2));
		callText.setText(item.getText(3));
		frequencyText.setText(item.getText(4));
		RSTSentSpinner.setSelection(Integer.parseInt(item.getText(5)));
		RSTReceivedSpinner.setSelection(Integer.parseInt(item.getText(6)));
		modeCombo.setText(item.getText(7));
		powerText.setText(item.getText(8));
		commentsText.setText(item.getText(9));
		addQSOButton.setText("Modify");
		deleteQSOButton.setEnabled(true);
		cancelButton.setEnabled(true);
	}

	
	private void clearForm() {
		contactNumberText.setText("");
		dateText.setText("");
		timeText.setText("");
		callText.setText("");
		RSTSentSpinner.setSelection(59);
		RSTReceivedSpinner.setSelection(59);
		frequencyText.setText("");
		powerText.setText("");
		commentsText.setText("");
		cancelButton.setEnabled(false);
		deleteQSOButton.setEnabled(false);
		addQSOButton.setText("Add");
		table.deselectAll();
		table.setTopIndex(0);
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
	
	
}
